package roboGP;

/**
 * Rappresenta i nodi del'albero che si traducono in condizioni per il robot.
 * Ha due figli obbligatori.
 */
public class ConditionNode implements ITreeNode 
{

	private String _javaCode;
	private ITreeNode _rightChild;
	private ITreeNode _leftChild;
	private ITreeNode _parent;
	
	public ConditionNode()
	{
		_javaCode = null;
		_rightChild = null;
		_leftChild = null;
		_parent = null;
	}
	
	public ConditionNode(String code, ITreeNode parent, ITreeNode left, ITreeNode right)
	{
		_javaCode = code;
		_rightChild = right;
		_leftChild = left;
		_parent = parent;
	}
	
	public void setParent(ITreeNode node)
	{
		_parent = node;
	}
	
	public ITreeNode getParent()
	{
		return _parent;
	}
	
	public void setChildren(ITreeNode left, ITreeNode right)
	{
		_leftChild = left;
		_rightChild = right;
	}
	
	public void setLeftChild(ITreeNode left)
	{
		_leftChild = left;
	}
	
	public void setRightChild(ITreeNode right)
	{
		_rightChild = right;
	}
	
	public ITreeNode getRightChild()
	{
		return _rightChild;
	}
	
	public ITreeNode getLeftChild()
	{
		return _leftChild;
	}
	
	public boolean hasChildren()
	{
		return (_rightChild != null) && (_leftChild != null);
	}
	
	public void setJavaCode(String code)
	{
		_javaCode = code;
	}
	
	public String getJavaCode() 
	{
		return _javaCode;
	}
	
	public int getDepth()
	{
		return recGetDepth(this);
	}
	
	private int recGetDepth(ITreeNode node)
	{
		if (node.getParent() == null)
			return 0;
		
		return 1 + recGetDepth(node.getParent());
	}
}
